﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Untitled Page</title>
    <style type="text/css">

.style1 {color: #63b4cd}
H2 {
	MARGIN-TOP: 25px; FONT-SIZE: 16px; MARGIN-BOTTOM: 0px
}
H2 {
	FONT-FAMILY: "Lucida Grande",  Tahoma, Arial, sans-serif;
	text-transform: capitalize;
}
.postdate{
PADDING-RIGHT: 0px;
	PADDING-BOTTOM: 12px;
	PADDING-TOP: 0px;
	font-size: 9px;
}
A {
	COLOR: #8ab459; TEXT-DECORATION: none
}
P {
	MARGIN: 0.5em 0px 1.5em; LINE-HEIGHT: 1.5em
}
    </style>
</head>
<body>

    <div class="posttitle">
        <h2 class="style1">
            <strong>Javascript Hash关联数组操作</strong></h2>
    </div>
    <div class="postdate">
        Submitted by <a href="http://www.medcl.net/blog/?action=showuser&amp;id=2">medcl</a> 
        on 2007, November 20, 3:35 AM.
        <a href="http://www.medcl.net/blog/?cid=4&amp;cname=%E2%99%82Collections">
        ♂Collections</a></div>
    <div class="postentry">
        标准javascript 是内含支持hash关联数组，经查找资料并测试，有关标准javascript内含的hash关联数组操作备忘如下 
        <p>
            <strong>１。Hash关联数组定义</strong></p>
        <p>
            // 定义空数组 myhash = { }</p>
        <p>
            // 直接定义数组</p>
        <p>
            myhash = {”key1″:”val1″, “key2″:”val2″ }</p>
        <p>
            // 用Array 定义数组 
        </p>
        <p>
            myhash = new Array();<br />
            myhash[”key1″] = “val1″;<br />
            myhash[”key2″] = “val2″;</p>
        <p>
            <strong>２。向Hash关联数组添加键值</strong></p>
        <p>
            // 添加一个新键 newkey ，键值为 newval</p>
        <p>
            myhash[”newkey”] = “newval”;</p>
        <p>
            <strong>３。删除Hash关联数组已有键值</strong></p>
        <p>
            // 删除一个键 newkey ，同时，该键值对应的 newval 也就消失了。<br />
            delete myhash[”newkey”];</p>
        <p>
            <strong>４。遍历Hash关联数组</strong></p>
        <p>
            // 遍历整个hash 数组&nbsp;<br />
            for (key in myhash) {<br />
            val = myhash[key];<br />
            }</p>
        <p>
            <strong>５。Hash关联数组简易使用示例</strong></p>
        <p>
            // 转向脚本&nbsp;<br />
            &lt;script type=”text/javascript”&gt;<br />
            urlhash = { “yahoo”:”<a href="http://www.yahoo.cn/">www.yahoo.cn</a>“,<br />
            “baidu”:”<a href="http://www.baidu.com/">www.baidu.com</a>“,<br />
            “google”:”<a href="http://www.google.cn/">www.google.cn</a>” };</p>
        <p>
            // 交互式使用示例&nbsp;<br />
            userinfo = prompt(”请输入您最想去的搜索引擎：(yahoo|baidu|google)”, “yahoo”);<br />
            document.write(”您的选择：” + userinfo + “，&lt;a href=http://” + getURL(userinfo) + ” 
            target=_blank&gt;” + “按此即可进入” + “&lt;/a&gt;” + userinfo + “。”);</p>
        <p>
            // getURL&nbsp;<br />
            // 如果参数未定义，默认返回 <a href="http://www.yahoo.cn/">www.yahoo.cn</a> 网址<br />
            // @param choice 选择名称<br />
            // @return url 实际的URL<br />
            function getURL(choice) {<br />
            url = urlhash[choice];<br />
            if (typeof(urlhash[choice]) == “undefined”)<br />
            url = “<a href="http://www.yahoo.cn/">www.yahoo.cn</a>“;<br />
            return url;<br />
            }</p>
        <p>
            // 获得hash列表的所有 keys<br />
            // @param hash hash数组<br />
            // @return keys 键名数据<br />
            function array_keys(hash) {<br />
            keys = [];<br />
            for (key in hash)<br />
            keys.push(key);<br />
            return keys;<br />
            }<br />
            &lt;/script&gt;</p>
        <p>
        </p>
        <hr />
        <p>
        </p>
        &lt;script type=&quot;text/javascript&quot;&gt;
        <br />
        &lt;!--
        <br />
        &nbsp;&nbsp;&nbsp; Array.prototype.delRepeat=function(){
        <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //tempArr：重复项标志,tempArr1：保存重复数组项 
        <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var tempArr=[],tempArr1=[],j=0;
        <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //遍历数组 
        <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(var i=this.length;i&gt;0;i--){
        <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //如果该项还没有重复项 
        <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(!tempArr[this[i-1]]){
        <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tempArr[this[i-1]]=1;&nbsp;&nbsp;&nbsp; //通过关联数组方式标志该项并置为1（代表出现一次） 
        <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }else{
        <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(tempArr[this[i-1]]++==1)tempArr1[j++]=this[i-1];&nbsp;&nbsp;&nbsp; 
        //如果第一次重复则将该重复项保存，并将重复标志+1
        <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.splice(i-1,1);&nbsp;&nbsp;&nbsp; //从原数组删除该项(因为是倒序遍历，所以可以直接在此删除)
        <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
        <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
        <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return tempArr1;&nbsp;&nbsp;&nbsp; //返回重复项列表 
        <br />
        &nbsp;&nbsp;&nbsp; }
        <br />
        &nbsp;&nbsp;&nbsp; var a=new Array(5,410,52,&quot;,&quot;,24,5,&quot;,&quot;,true,false,24,24,25,5)
        <br />
        &nbsp;&nbsp;&nbsp; alert(&quot;原数组:&quot;+a);
        <br />
        &nbsp;&nbsp;&nbsp; alert(&quot;重复项:&quot;+a.delRepeat());
        <br />
        &nbsp;&nbsp;&nbsp; alert(&quot;剩余项:&quot;+a);
        <br />
        //--&gt;
        <br />
        &lt;/script&gt;
        <br />
    </div>
<script type="text/javascript">



</script>
<script src="../js组件/TongLibrary.js" type="text/javascript"></script>   
<script type="text/javascript">
/// <reference path="TongLibrary.js" />
//调用Tong.TongAjax类的数据
var ajax= new Tong.TongAjax.Ajax()
ajax.get("jquery/JqueryAndAjaxPro.aspx",
function(obj)
    {
        alert(obj.responseText);
    }

);

alert(Tong.TongUse.DelHtml("<span>sdfsdfsd</apa>"));
//调用Tong的方法结束

/*
            Ajax:function AJAXRequest(){//定义实现ajax方法的结口
                this.get=function(url,callback){},
                //实现post方法的接口
                this.post=function(url,value,callback){}*/

//start hash obj
var myhash={};
//add value to hash obj
myhash  = new Array();
myhash["k1"] ="v1";
myhash["k2"] ="a2";
//alert(myhash["k1"]);
//del hash obj
delete myhash;


//easy to usr hash array
var ehash ={
日本:function(o)
        {
        
            alert(o);
        },
"baidu":"www.baidu.com",
"google":"www.google.com",
sun:"www.sun.com"        
}

ehash
//alert(ehash.sun);

//得到hash时里所有的键
for(key in ehash)
{
    //alert(key);提示
}


</script>
</body>
</html>
